import Vue from 'vue'
import App from './App'
import common from './common/common.js'

Vue.config.productionTip = false

App.mpType = 'app'

const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}


const accSub = (arg1,arg2) =>
{    
    return accAdd(arg1,-arg2);
}

const accAdd = (arg1, arg2) =>
{
  var r1, r2, m, c;
  try { r1 = arg1.toString().split(".")[1].length; } catch (e) { r1 = 0; }
  try { r2 = arg2.toString().split(".")[1].length; } catch (e) { r2 = 0; }
  c = Math.abs(r1 - r2);
  m = Math.pow(10, Math.max(r1, r2));
  if (c > 0) {
      var cm = Math.pow(10, c);
      if (r1 > r2) {
          arg1 = Number(arg1.toString().replace(".", ""));
          arg2 = Number(arg2.toString().replace(".", "")) * cm;
      } else {
          arg1 = Number(arg1.toString().replace(".", "")) * cm;
          arg2 = Number(arg2.toString().replace(".", ""));
      }
  } else {
      arg1 = Number(arg1.toString().replace(".", ""));
      arg2 = Number(arg2.toString().replace(".", ""));
  }
  return (arg1 + arg2) / m;
}

const accMul = (arg1,arg2)=>
{
  var m=0,s1=arg1.toString(),s2=arg2.toString();
  try{m+=s1.split(".")[1].length}catch(e){}
  try{m+=s2.split(".")[1].length}catch(e){}
  return Number(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m)
}

const accDiv = (arg1, arg2) =>
{
//     var t1 = 0, t2 = 0, r1, r2;
//     try { t1 = arg1.toString().split(".")[1].length; } catch (e) {}
//     try { t2 = arg2.toString().split(".")[1].length; } catch (e) {}
//     with (Math) {
//         r1 = Number(arg1.toString().replace(".", ""));
//         r2 = Number(arg2.toString().replace(".", ""));
//         return (r1 / r2) * Math.pow(10, t2 - t1);
//     }
		return accMul(arg1,1/arg2);
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

//提示
const tipMsg =(title,icon,time,mask)=>{
        title = title == undefined ? "出错了" : title;
        icon = icon == undefined ? "none" : icon;
        time = time == undefined ? 1500 : time;
        mask = mask == undefined ? true : mask;
        uni.showToast({
            title:title,
            icon:icon,
            mask:mask,
            duration:time
        })
}

const loading = (title,icon,mask)=>{
        title = title == undefined ? "加载中..." : title;
        icon = icon == undefined ? "none" : icon;
        mask = mask == undefined ? true : mask;
        uni.showLoading({
        					title: title,
        					mask: mask,
        					icon:icon
        				})
}

/**
	 * 获取任意时间
	 */
const getAnyDate = (date, AddDayCount = 0)=> {
		if (!date) {
			date = new Date()
		}
		if (typeof date !== 'object') {
			date = date.replace(/-/g, '/')
		}
		const dd = new Date(date)
		dd.setDate(dd.getDate() + AddDayCount) // 获取AddDayCount天后的日期

		const y = dd.getFullYear()
		const m = dd.getMonth() + 1 < 10 ? '0' + (dd.getMonth() + 1) : dd.getMonth() + 1 // 获取当前月份的日期，不足10补0
		const d = dd.getDate() < 10 ? '0' + dd.getDate() : dd.getDate() // 获取当前几号，不足10补0
		const h = (dd.getHours() < 10 ? '0'+(dd.getHours()) : dd.getHours())
		const f = (dd.getMinutes() < 10 ? '0'+(dd.getMinutes()) : dd.getMinutes())
		const s = (dd.getSeconds() < 10 ? '0'+(dd.getSeconds()) : dd.getSeconds())
		return {
			fullDate: y + '-' + m + '-' + d,
			timeDate: y + '-' + m + '-' + d + ' ' + h + ':' + f + ':' + s,
			year: y,
			month: m,
			date: d,
			day: dd.getDay()
		}
	} 
	
const tellUserLogin = () => {
				uni.showModal({
				  title: '您还未登录',
				  content: '现在去登录吗',
				  success (res) {
				      if (res.confirm) {
						  uni.navigateTo({ url: '../my/login/login' })
				      } 
				    }
				})
			}

const addItemToCart = (map) =>{		  
	  if (map.customerId== '') {
		tellUserLogin()
		return
	  }
	  if(map.number == 0){
		  tipMsg("购买数不能是0")
		  return
	  }
	uni.request({
	  url: Vue.prototype.$common.serverAddr+'/sales/cart/add',
	  data: map,
	  method: 'POST',
	  success: function(res) {
		var tip = res.data.status == 200 ? '加入购物车成功' : '加入购物车失败'
		tipMsg(tip)
	  }
	})
}

Vue.prototype.$uniApi={tipMsg, loading, getAnyDate, formatTime,
accSub, accAdd, accMul, accDiv, formatNumber, tellUserLogin, addItemToCart};

Vue.prototype.$common = common;




Vue.filter("formatDate",(ts)=>{
	let date = new Date(ts)
	//时间戳为10位需*1000，时间戳为13位的话不需乘1000
	let y = date.getFullYear();
	let MM = date.getMonth() + 1;
	MM = MM < 10 ? ('0' + MM) : MM; //月补0
	let d = date.getDate();
	d = d < 10 ? ('0' + d) : d; //天补0
	let h = date.getHours();
	h = h < 10 ? ('0' + h) : h; //小时补0
	let m = date.getMinutes();
	m = m < 10 ? ('0' + m) : m; //分钟补0
	let s = date.getSeconds();
	s = s < 10 ? ('0' + s) : s; //秒补0
	return y + '-' + MM + '-' + d + ' ' + h + ':' + m+ ':' + s; 
})


const app = new Vue({
	...App
})
app.$mount()
